#include <iostream>
using namespace std;

class Resource {
public:
  Resource() {
    cout << this << "->Resource\n";
  }

  ~Resource() {
    cout << this << "->~Resource\n";
  }
};

template<typename T>
class Handle {
  T* m_ptr;
public:
  explicit Handle(T* p = nullptr) : m_ptr(p) {}

  void release() {
    delete m_ptr;
    m_ptr = nullptr;
  }

  ~Handle() {
    delete m_ptr;
  }

  Handle(const Handle&) = delete;
  Handle& operator=(const Handle&) = delete;

  Handle(Handle&& h) : m_ptr(h.m_ptr) {
    h.m_ptr = nullptr;
  }

  Handle& operator=(T* p) {
    if(m_ptr != p) {
      this->release();
      m_ptr = p;
    }
    return *this;
  }

  Handle& operator=(Handle&& h) {
    if(this != &h) {
      this->release();
      m_ptr = h.m_ptr;
      h.m_ptr = nullptr;
    }
    return *this;
  }

  void swap(Handle& h) noexcept {
    auto p = m_ptr;
    m_ptr = h.m_ptr;
    h.m_ptr = p;
  }
};

int main() {
  {
    Handle<Resource> h1;
    h1 = new Resource();
    Handle<Resource> h2;
    h2 = std::move(h1);
  }
}
